package com.cn.customer.housekeeping.housekeepingc.ui;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;

import com.cn.customer.housekeeping.housekeepingc.R;
import com.cn.customer.housekeeping.housekeepingc.bean.BaseResp;
import com.cn.customer.housekeeping.housekeepingc.ui.base.BaseFragmentActivity;
import com.cn.customer.housekeeping.housekeepingc.ui.base.BaseFragmentAdapter;
import com.cn.customer.housekeeping.housekeepingc.ui.fragment.MerchandiseListBaseFragment;

import java.util.ArrayList;
import java.util.List;




public class MerchandiseListActivity extends BaseFragmentActivity {

    private List<String> mTitles = new ArrayList<String>() {
        {
            add("全部");
            add("待付款");
            add("进行中");
            add("已完成");
            add("待评价");

        }
    };
    @Override
    public void setHeader() {
        super.setHeader();
        rlTitle.setVisibility(View.VISIBLE);
        tvTopTitle.setText("订单");

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_merchandise_list);
        super.onCreate(savedInstanceState);
    }
    ViewPager viewPager;
    TabLayout tabLayout;
    @Override
    public void initViews() {
        viewPager=(ViewPager)findViewById(R.id.viewpager);
        tabLayout=(TabLayout)findViewById(R.id.tab_layout);
        initSelect();
        initViewPager();

    }

    @Override
    public void initListeners() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void onError(BaseResp s) {

    }

    @Override
    public void onSuccess(BaseResp s) {

    }




    /**
     * 初始化水平选择栏
     */
    private void initSelect() {
        //设置TabLayout的模式
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        //需要自定义布局时，需要在绑定viewpager前加入布局
        for (int i = 0; i < mTitles.size(); i++) {
            tabLayout.addTab(tabLayout.newTab().setText(mTitles.get(i)));
        }
    }


    /**
     * 初始化ViewPager
     */
    private void initViewPager() {
        final ArrayList<Fragment> fragmentList = new ArrayList<>();
        MerchandiseListBaseFragment merchandiseListAll = MerchandiseListBaseFragment.
                newInstance(0);
        MerchandiseListBaseFragment merchandiseListDisPatch = MerchandiseListBaseFragment.
                newInstance(1);
        MerchandiseListBaseFragment merchandiseListReceive = MerchandiseListBaseFragment.
                newInstance(2);
        MerchandiseListBaseFragment merchandiseListFinish = MerchandiseListBaseFragment.
                newInstance(3);
        MerchandiseListBaseFragment merchandiseList4 = MerchandiseListBaseFragment.
                newInstance(4);
        fragmentList.add(merchandiseListAll);
        fragmentList.add(merchandiseListDisPatch);//
        fragmentList.add(merchandiseListReceive);//
        fragmentList.add(merchandiseListFinish);//
        fragmentList.add(merchandiseList4);//

        FragmentPagerAdapter fragmentAdapter = new BaseFragmentAdapter(getSupportFragmentManager(), fragmentList, mTitles);
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(4);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.getTabAt(position).select();
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
//                ((MerchandiseListBaseFragment) fragmentList.get(tab.getPosition())).fresh();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

}
